﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Rhino_Mocks.Utilities;
using Rhino_Mocks.Models;
using System.Text;

namespace Rhino_Mocks.Controllers {
    public class HomeController : Controller {
        private HttpContextBase _httpContext;
        private CustomerSetting _customerSetting;
        public HomeController(HttpContextBase httpContext, CustomerSetting customerSetting)
        {
            this._httpContext = httpContext;
            this._customerSetting = customerSetting;
        }
        /// <summary>
        /// 验证返回的错误提示
        /// </summary>
        public string ModelError
        {
            get
            {
                StringBuilder sb = new StringBuilder();
                foreach (var item in ModelState.Values)
                {
                    foreach (var er in item.Errors)
                    {
                        if (!string.IsNullOrEmpty(er.ErrorMessage))
                            sb.Append(er.ErrorMessage + ",");
                        else
                            sb.Append(er.Exception.Message + ",");
                    }
                }
                return sb.ToString().TrimEnd(',');
            }
        }
        public ActionResult Index()
        {
            bool isAdmin = _customerSetting.IsAdmin;
            var httpContext = _httpContext.Request;
            var result = new ResultData { Id = 1234, Name = "商品001" };
            Singleton singleton = Singleton.Instance;
            Singleton singleton1 = Singleton.Instance;
            bool isEqual = singleton.Equals(singleton1);
            bool objEqual = (singleton.SearchData().Equals(singleton1.SearchData()));
            return View(result);
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        [HttpPost]
        public JsonResult CheckLog(UserInfoModel model)
        {
            var data = new { success = true, msg = "" };
            if (!ModelState.IsValid)
            {
                data = new { success = false, msg = ModelError };
            }
            return Json(data);
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        public JsonResult GetJsonData()
        {
            var data = new List<ResultData> { new ResultData { Id = 1, Name = "apple电脑" }, new ResultData { Id = 2, Name = "华为电脑" } };
            return Json(data, JsonRequestBehavior.AllowGet);
        }
    }

    public class ResultData {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public class Singleton {
        private Singleton() { }
        static Singleton()
        {
            instance = new Singleton();
        }
        static readonly Singleton instance;
        public static Singleton Instance
        {
            get { return instance; }
        }
        public ResultData SearchData()
        {
            return new ResultData();
        }
    }
}